﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Sohobloo.SoCommon;
using Sohobloo.SoCommon.Log;
using Sohobloo.SoMcCoffee.Entity;

namespace Mojito.WebSite.Controllers
{
    public class ApiControllerBase : ApiController
    {
        #region Logger
        [Import]
        private ILogger Logger { get; set; }

        private string getCallingMethod()
        {
            var frame = new StackFrame(2);
            return frame.GetMethod().DeclaringType.Name + "." + frame.GetMethod().Name;
        }

        public void LogError(string msg, Exception ex = null)
        {
            msg = getCallingMethod() + "\r\n" + msg;
            if (ex == null)
            {
                Logger.Error(msg);
            }
            else
            {
                Logger.Error(msg, ex);
            }
        }

        public void LogInfo(string msg)
        {
            msg = getCallingMethod() + "\r\n" + msg;
            Logger.Info(msg);
        }

        public void LogDebug(string msg)
        {
            msg = getCallingMethod() + "\r\n" + msg;
            Logger.Debug(msg);
        }
        #endregion

        protected HttpStatusCode GetStatusCode(HttpMethod method, OperationResult operationResult)
        {
            switch (operationResult.ResultType)
            {
                case OperationResultType.Unknown:
                    return HttpStatusCode.InternalServerError;
                case OperationResultType.UserDefine:
                    return (HttpStatusCode) 430;
                case OperationResultType.Warning:
                case OperationResultType.Fail:
                case OperationResultType.Success:
                    if (method == HttpMethod.Delete)
                    {
                        return HttpStatusCode.NoContent;
                    }
                    if (method == HttpMethod.Get)
                    {
                        var t = operationResult.AppendData as IEnumerable<EntityBase>;
                        if (t == null)
                        {
                            //t为null，说明是请求单个实体，且没有找到数据
                            return HttpStatusCode.OK;
                        }
                        //t不为null，说明请求的是集合
                        return t.Any() ? HttpStatusCode.OK : HttpStatusCode.NoContent;
                    }
                    return HttpStatusCode.Created;
                case OperationResultType.Error:
                    return (HttpStatusCode)530;
                case OperationResultType.Conflict:
                    return HttpStatusCode.Conflict;
                case OperationResultType.NoChange:
                    return HttpStatusCode.NotModified;
                case OperationResultType.Exist:
                    return (HttpStatusCode)430;
                case OperationResultType.NotExist:
                    return method == HttpMethod.Post ? HttpStatusCode.BadRequest : HttpStatusCode.NotFound;
                default:
                    LogError("OperationResultType => HttpStatusCode not matched");
                    return HttpStatusCode.InternalServerError;
            }
        }
    }
}
